package cn.tedu._03mvcboot02.controller;

import cn.tedu._03mvcboot02.mapper.UserMapper;
import cn.tedu._03mvcboot02.pojo.dto.UserInsertDTO;
import cn.tedu._03mvcboot02.pojo.entity.User;
import cn.tedu._03mvcboot02.pojo.vo.UserListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
@RequestMapping("/v1/users")
//@Controller
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("insert")
    @ResponseBody
    public String insert(UserInsertDTO userInsertDTO) {
        User user=new User();
        //框架不允许我们做这么蠢的事情，所以有一个复制属性的方法
//        user.setUsername(userInsertDTO.getUsername());
//        user.setPassword(userInsertDTO.getPassword());
//        user.setNickname(userInsertDTO.getNickname());
        BeanUtils.copyProperties(userInsertDTO,user);
        user.setCreated(new Date());

        userMapper.insertUser(user);
        return "添加成功";
    }

    @RequestMapping("userlist")
    @ResponseBody
    public List<UserListVo> userList(){
        return userMapper.selectUserList();
    }
    @RequestMapping("delete")
    @ResponseBody
    public String delete(int id){
        userMapper.deleteUserById(id);
        return "删除成功";
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(User user){
        userMapper.updateUserById(user);
        return "修改成功";
    }
}

